package com.vitabuybuy.tv.ui.mine.order.after

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.adaptebinding.ImageAdapter
import com.vitabuybuy.tv.databinding.ActivityAfterSaleTypeBinding
import com.vitabuybuy.tv.dto.OrderDetailDto
import com.library.common.base.BaseActivity
import com.library.common.ext.setOnclickNoRepeat
import com.vitabuybuy.tv.ui.mine.order.after.viewmodel.AfterSaleViewModel
import kotlinx.android.synthetic.main.activity_after_sale_type.*

class AfterSaleTypeActivity : BaseActivity<AfterSaleViewModel, ActivityAfterSaleTypeBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_after_sale_type

    private var itemBean: OrderDetailDto.ItemListBean? = null

    override fun onGetBundle(bundle: Bundle?) {
        itemBean = bundle?.getSerializable("itemBean") as OrderDetailDto.ItemListBean
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.after_sale_type))
        tvCommodityTitle.text = itemBean?.product?.name
        tvType.text = itemBean?.specifications
        ImageAdapter.setNomalImageUrl(
            ivImage,
            itemBean?.goods?.image,
            ContextCompat.getDrawable(mContext, R.drawable.default_image)
        )
    }

    override fun eventHandling() {
        setOnclickNoRepeat(rlReturnMoney, rlReturnGoodsMoney){v->
            when (v) {
                rlReturnMoney -> {
                    itemBean?.run {
                        RequestRefundActivity.openActivity(
                            mContext,
                            this,
                            -1,
                            RequestRefundActivity.AFTER_SALE_TYPE_BY_ONLY_RETUAN_MONEY
                        )
                    }
                }
                rlReturnGoodsMoney -> {
                    itemBean?.run {
                        RequestRefundActivity.openActivity(
                            mContext,
                            this,
                            -1,
                            RequestRefundActivity.AFTER_SALE_TYPE_BY_RETURN_GOODS_AND_RETUAN_MONEY
                        )
                    }
                }
            }
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) return
        if (requestCode == 1001) finish()
    }

    companion object {
        fun openActivity(
            mContext: BaseActivity<*, *>,
            itemBean: OrderDetailDto.ItemListBean
        ) {
            mContext.startActivity(
                bundleOf(Pair("itemBean", itemBean)),
                AfterSaleTypeActivity::class.java
            )
        }
    }
}
